package com.panda.controller.admin;

import com.panda.entity.po.User;
import com.panda.service.RoleService;
import com.panda.service.UserService;
import com.panda.utils.RespondResult;
import com.panda.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/user")
public class AdminController {

    private final UserService userService;

    private final RoleService roleService;

    @Autowired
    public AdminController(UserService userService, RoleService roleService) {
        this.userService = userService;
        this.roleService = roleService;
    }

    @GetMapping("info")
    public RespondResult getUserInfo(){
        Long userId = SecurityUtils.getUserId();
        return userService.getUserInfo(userId);
    }

    @GetMapping("role")
    public RespondResult getRoleName(){
        return roleService.getRoleName();
    }

    @GetMapping("routes")
    public RespondResult getRoleRoute(){
        return roleService.getRoleRoutes();
    }

    @PutMapping("editInfo")
    public RespondResult editUserInfo(@RequestBody User user){
//        在控制层做过滤操作，让service层更加专注实际业务需求处理
        if (!user.getId().equals(SecurityUtils.getUserId())){
            return RespondResult.errorResult(400,"不能修改他人信息");
        }
        return userService.updateUserSex(user);
    }
    @PutMapping("editAvatar")
    public RespondResult editUserAvatar(@RequestBody User user){
        if (!user.getId().equals(SecurityUtils.getUserId())){
            return RespondResult.errorResult(400,"不能修改他人信息");
        }
        return userService.updateUserAvatar(user);
    }
}
